﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Changeset.cs" company="Mar3ek">
//   http://mar3ek.wordpress.com
// </copyright>
// <summary>
//   
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace LighthouseNET
{
    using System;
    using System.Xml.Linq;

    /// <summary>
    /// Class representing a changeset
    /// </summary>
    public class Changeset
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Changeset"/> class.
        /// </summary>
        /// <param name="body">The body.</param>
        /// <param name="title">The title.</param>
        /// <param name="changedAt">The time of commiting od this changeset.</param>
        /// <param name="changes">The changes.</param>
        /// <param name="revision">The sourcecontrol revision.</param>
        public Changeset(string body, string title, DateTime changedAt, string changes, int revision)
        {
            Body = body;
            Title = title;
            ChangedAt = changedAt;
            Changes = changes;
            Revision = revision;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Changeset"/> class. 
        /// </summary>
        /// <param name="body">
        /// The body.
        /// </param>
        /// <param name="bodyHtml">
        /// The body HTML.
        /// </param>
        /// <param name="changedAt">
        /// The changed at.
        /// </param>
        /// <param name="changes">
        /// The changes.
        /// </param>
        /// <param name="projectId">
        /// The project id.
        /// </param>
        /// <param name="revision">
        /// The revision.
        /// </param>
        /// <param name="title">
        /// The title.
        /// </param>
        /// <param name="userId">
        /// The user id.
        /// </param>
        private Changeset(
            string body, 
            string bodyHtml,
            DateTime changedAt,
            string changes, 
            int projectId,
            int revision,
            string title,
            int userId)
        {
            Body = body;
            Title = title;
            ChangedAt = changedAt;
            Changes = changes;
            Revision = revision;
            BodyHtml = bodyHtml;
            ProjectId = projectId;
            UserId = userId;
        }

        #region Properties
        /// <summary>
        /// Gets the body.
        /// </summary>
        public string Body { get; private set; }

        /// <summary>
        /// Gets the body as HTML.
        /// </summary>
        public string BodyHtml { get; private set; }

        /// <summary>
        /// Gets the time of commiting.
        /// </summary>
        public DateTime ChangedAt { get; private set; }

        /// <summary>
        /// Gets the changes.
        /// </summary>
        public string Changes { get; private set; }

        /// <summary>
        /// Gets the project id.
        /// </summary>
        public int ProjectId { get; private set; }

        /// <summary>
        /// Gets the revision.
        /// </summary>
        public int Revision { get; private set; }

        /// <summary>
        /// Gets the title.
        /// </summary>
        public string Title { get; private set; }

        /// <summary>
        /// Gets the user id.
        /// </summary>
        public int UserId { get; private set; } 
        #endregion

        /// <summary>
        /// Parses the specified element.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <returns>Parsed chengeset</returns>
        /// <exception cref="ArgumentNullException">Element is null or empty.</exception>
        internal static Changeset Parse(XElement element)
        {
            if (element == null || element.IsEmpty)
            {
                throw new ArgumentNullException("element");
            }

            // ReSharper disable PossibleNullReferenceException
            var body = element.Element("body").Value;

            var bodyHtml = element.Element("body-html").Value;
            var changedAt = Globals.DateTimeFromLighthouse(element.Element("changed-at").Value);
            var changes = element.Element("changes").Value;
            var projectId = int.Parse(element.Element("project-id").Value);
            var revision = int.Parse(element.Element("revision").Value);
            var title = element.Element("title").Value;
            var userId = int.Parse(element.Element("user-id").Value);

            // ReSharper restore PossibleNullReferenceException
            return new Changeset(
                body,
                bodyHtml,
                changedAt,
                changes,
                projectId,
                revision,
                title,
                userId);
        }
    }
}